﻿using System;
using System.Data;
using System.Text;

using Discuz.Common;
using Discuz.Forum;
using Discuz.Config;
using Discuz.Entity;
using Discuz.Web.UI;
using Discuz.Plugin.PasswordMode;

namespace Discuz.Web
{
    /// <summary>
    /// 登录
    /// </summary>
    public class tylogin : PageBase
    {
        public string m_ResponseText;
        protected override void ShowPage()
        {
            string userName = DNTRequest.GetString("User");
            if (Login(userName))
            {
                m_ResponseText = "登录成功";

                this.Load += delegate
                {
                    Response.Redirect("Index.aspx");
                    this.Load += delegate { };
                };
            }
            else
            {
                m_ResponseText = "登录失败，您输入的员工工号可能不正确";
            }

        }
        public bool Login(string UserName)
        {
            if (!string.IsNullOrEmpty(UserName))
            {
                int uid = Users.GetUserId(UserName);
                if (uid > 0)
                {

                    ForumUtils.WriteUserCookie(uid, TypeConverter.StrToInt(DNTRequest.GetString("expires"), -1), config.Passwordkey);
                    oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout, uid, "");
                    olid = oluserinfo.Olid;
                    OnlineUsers.UpdateAction(olid, UserAction.Login.ActionID, 0);
                    //删除之前的错误登录信息
                    LoginLogs.DeleteLoginLog(DNTRequest.GetIP());
                    Users.UpdateUserCreditsAndVisit(uid, DNTRequest.GetIP());
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }


        }
    }
}